﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.SqlClient;
using Help;
using System.Data;

namespace Dal
{
    public class MenuDal
    {
        public MenuDal()
		{}
		#region  BasicMethod
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int MenuID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Menu");
			strSql.Append(" where MenuID=@MenuID");
			SqlParameter[] parameters = {
					new SqlParameter("@MenuID", SqlDbType.Int,4)
			};
			parameters[0].Value = MenuID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(MenuModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Menu(");
			strSql.Append("MenuTypeID,MenuName,Pid,MenuFlag,HtmlFaicon,HtmlUrl,HtmlTabid,HtmlRank)");
			strSql.Append(" values (");
			strSql.Append("@MenuTypeID,@MenuName,@Pid,@MenuFlag,@HtmlFaicon,@HtmlUrl,@HtmlTabid,@HtmlRank)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@MenuTypeID", SqlDbType.Int,4),
					new SqlParameter("@MenuName", SqlDbType.VarChar,50),
					new SqlParameter("@Pid", SqlDbType.Int,4),
					new SqlParameter("@MenuFlag", SqlDbType.Bit,1),
					new SqlParameter("@HtmlFaicon", SqlDbType.VarChar,50),
					new SqlParameter("@HtmlUrl", SqlDbType.VarChar,50),
					new SqlParameter("@HtmlTabid", SqlDbType.VarChar,50),
					new SqlParameter("@HtmlRank", SqlDbType.Int,4)};
			parameters[0].Value = model.MenuTypeID;
			parameters[1].Value = model.MenuName;
			parameters[2].Value = model.Pid;
			parameters[3].Value = model.MenuFlag;
			parameters[4].Value = model.HtmlFaicon;
			parameters[5].Value = model.HtmlUrl;
			parameters[6].Value = model.HtmlTabid;
			parameters[7].Value = model.HtmlRank;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(MenuModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Menu set ");
			strSql.Append("MenuTypeID=@MenuTypeID,");
			strSql.Append("MenuName=@MenuName,");
			strSql.Append("Pid=@Pid,");
			strSql.Append("MenuFlag=@MenuFlag,");
			strSql.Append("HtmlFaicon=@HtmlFaicon,");
			strSql.Append("HtmlUrl=@HtmlUrl,");
			strSql.Append("HtmlTabid=@HtmlTabid,");
			strSql.Append("HtmlRank=@HtmlRank");
			strSql.Append(" where MenuID=@MenuID");
			SqlParameter[] parameters = {
					new SqlParameter("@MenuTypeID", SqlDbType.Int,4),
					new SqlParameter("@MenuName", SqlDbType.VarChar,50),
					new SqlParameter("@Pid", SqlDbType.Int,4),
					new SqlParameter("@MenuFlag", SqlDbType.Bit,1),
					new SqlParameter("@HtmlFaicon", SqlDbType.VarChar,50),
					new SqlParameter("@HtmlUrl", SqlDbType.VarChar,50),
					new SqlParameter("@HtmlTabid", SqlDbType.VarChar,50),
					new SqlParameter("@HtmlRank", SqlDbType.Int,4),
					new SqlParameter("@MenuID", SqlDbType.Int,4)};
			parameters[0].Value = model.MenuTypeID;
			parameters[1].Value = model.MenuName;
			parameters[2].Value = model.Pid;
			parameters[3].Value = model.MenuFlag;
			parameters[4].Value = model.HtmlFaicon;
			parameters[5].Value = model.HtmlUrl;
			parameters[6].Value = model.HtmlTabid;
			parameters[7].Value = model.HtmlRank;
			parameters[8].Value = model.MenuID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int MenuID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Menu ");
			strSql.Append(" where MenuID=@MenuID");
			SqlParameter[] parameters = {
					new SqlParameter("@MenuID", SqlDbType.Int,4)
			};
			parameters[0].Value = MenuID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string MenuIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Menu ");
			strSql.Append(" where MenuID in ("+MenuIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public MenuModel GetModel(int MenuID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 MenuID,MenuTypeID,MenuName,Pid,MenuFlag,HtmlFaicon,HtmlUrl,HtmlTabid,HtmlRank from Menu ");
			strSql.Append(" where MenuID=@MenuID");
			SqlParameter[] parameters = {
					new SqlParameter("@MenuID", SqlDbType.Int,4)
			};
			parameters[0].Value = MenuID;

			MenuModel model=new MenuModel();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public MenuModel DataRowToModel(DataRow row)
		{
            MenuModel model = new MenuModel();
			if (row != null)
			{
				if(row["MenuID"]!=null && row["MenuID"].ToString()!="")
				{
					model.MenuID=int.Parse(row["MenuID"].ToString());
				}
				if(row["MenuTypeID"]!=null && row["MenuTypeID"].ToString()!="")
				{
					model.MenuTypeID=int.Parse(row["MenuTypeID"].ToString());
				}
				if(row["MenuName"]!=null)
				{
					model.MenuName=row["MenuName"].ToString();
				}
				if(row["Pid"]!=null && row["Pid"].ToString()!="")
				{
					model.Pid=int.Parse(row["Pid"].ToString());
				}
				if(row["MenuFlag"]!=null && row["MenuFlag"].ToString()!="")
				{
					if((row["MenuFlag"].ToString()=="1")||(row["MenuFlag"].ToString().ToLower()=="true"))
					{
						model.MenuFlag=true;
					}
					else
					{
						model.MenuFlag=false;
					}
				}
				if(row["HtmlFaicon"]!=null)
				{
					model.HtmlFaicon=row["HtmlFaicon"].ToString();
				}
				if(row["HtmlUrl"]!=null)
				{
					model.HtmlUrl=row["HtmlUrl"].ToString();
				}
				if(row["HtmlTabid"]!=null)
				{
					model.HtmlTabid=row["HtmlTabid"].ToString();
				}
				if(row["HtmlRank"]!=null && row["HtmlRank"].ToString()!="")
				{
					model.HtmlRank=int.Parse(row["HtmlRank"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * FROM v_Menu ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" MenuID,MenuTypeID,MenuName,Pid,MenuFlag,HtmlFaicon,HtmlUrl,HtmlTabid,HtmlRank ");
			strSql.Append(" FROM Menu ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Menu ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.MenuID desc");
			}
			strSql.Append(")AS Row, T.*  from Menu T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Menu";
			parameters[1].Value = "MenuID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
    }
}
